//
//  ZXXMainTabBar.swift
//  Swift_Weibo
//
//  Created by 朱骁雄 on 16/4/8.
//  Copyright © 2016年 朱骁雄. All rights reserved.
//

import UIKit

class ZXXMainTabBar: UITabBar {
    // 定义闭包属性 ,传递
    var composeClosure : (() -> ())?

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let buttonCount = 5
        //宽度
        let width = self.frame.size.width / CGFloat(buttonCount)
        
        //创建frame 
        let newFrame = CGRect(x: 0, y: 0, width: width, height: frame.size.height)
        
        var index = 0
        
        for subView in subviews {
            
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            subView.frame = CGRectOffset(newFrame, CGFloat(index) * width, 0)
                
                index += index == 1 ? 2 : 1
            
            }
    
        }
        
        compostButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.size.height)
    }

    @objc private func didClickComposeButtonn(){
        
        composeClosure?()
    }


    private lazy var compostButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: Selector("didClickComposeButtonn"), forControlEvents: .TouchUpInside)
        
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button")) forState:.)
        
        self.addSubview(button)
        
        return button
}()
}
